﻿#region Copyright (c) 2000-2013 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{                                                                   }
{                                                                   }
{       Copyright (c) 2000-2013 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2013 Developer Express Inc.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
namespace UColor.Core.Helpers {
	public class SelectHelper : DependencyObjectExt {
		class SelectCommandImpl : ICommand {
			SelectHelper selectHelper;
			public SelectCommandImpl(SelectHelper selectHelper) {
				this.selectHelper = selectHelper;
			}
			public bool CanExecute(object parameter) { return true; }
			public event EventHandler CanExecuteChanged { add { } remove { } }
			public void Execute(object parameter) {
				this.selectHelper.Select(parameter);
			}
		}
		class UnselectCommandImpl : ICommand {
			SelectHelper selectHelper;
			public UnselectCommandImpl(SelectHelper selectHelper) {
				this.selectHelper = selectHelper;
			}
			public bool CanExecute(object parameter) { return true; }
			public event EventHandler CanExecuteChanged { add { } remove { } }
			public void Execute(object parameter) {
				this.selectHelper.Unselect(parameter);
			}
		}
		#region Dependency Properties
		public static readonly DependencyProperty SelectedValueProperty;
		public static readonly DependencyProperty SelectCommandProperty;
		public static readonly DependencyProperty UnselectCommandProperty;
		static SelectHelper() {
			Type ownerType = typeof(SelectHelper);
			SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(object), ownerType, new PropertyMetadata(null, RaiseSelectedValueChanged));
			SelectCommandProperty = DependencyProperty.Register("SelectCommand", typeof(ICommand), ownerType, new PropertyMetadata(null, RaiseSelectCommandChanged));
			UnselectCommandProperty = DependencyProperty.Register("UnselectCommand", typeof(ICommand), ownerType, new PropertyMetadata(null, RaiseUnselectCommandChanged));
		}
		object selectedValueValue = null;
		ICommand selectCommandValue = null;
		ICommand unselectCommandValue = null;
		static void RaiseSelectedValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
			((SelectHelper)d).selectedValueValue = e.NewValue;
		}
		static void RaiseSelectCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
			((SelectHelper)d).selectCommandValue = (ICommand)e.NewValue;
		}
		static void RaiseUnselectCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
			((SelectHelper)d).unselectCommandValue = (ICommand)e.NewValue;
		}
		#endregion
		public SelectHelper() {
			SelectCommand = new SelectCommandImpl(this);
			UnselectCommand = new UnselectCommandImpl(this);
		}
		public object SelectedValue { get { return selectedValueValue; } set { SetValue(SelectedValueProperty, value); } }
		public void Select(object value) {
			SelectedValue = value;
		}
		public void Unselect(object value) {
			if(object.Equals(SelectedValue, value))
				SelectedValue = null;
		}
		#region Commands
		public ICommand SelectCommand { get { return selectCommandValue; } private set { SetValue(SelectCommandProperty, value); } }
		public ICommand UnselectCommand { get { return unselectCommandValue; } private set { SetValue(UnselectCommandProperty, value); } }
		#endregion
	}
}
